package uk.co.democode.db;

import uk.co.democode.exception.JavoidAppException;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

/**
 * Adapter to manipulate DB operations.
 * 
 * @author Zeyin
 */
public class DBAdapter {

	private Context context;
	private SQLiteDatabase db;

	/**
	 * Constructor with initial context
	 * 
	 * @param context
	 */
	public DBAdapter(Context context) {
		this.context = context;
		DBHelper dbHelper = new DBHelper(this.context);
		this.db = dbHelper.getWritableDatabase();
	}
	
	/**
	 * close db
	 */
	public void close() {
		db.close();
	}
	
	/**
	 * Given table name, names, values, and insert one row into
	 * DB and return the row id
	 * 
	 * @param tableName
	 * @param names
	 * @param values
	 * @return inserted row id
	 * @throws JavoidAppException
	 */
	public long insert(String tableName, String names[], String values[]) 
			throws JavoidAppException {
		String stringStatement = getInsertStatement(tableName, names, values);
		SQLiteStatement insertStatement = db.compileStatement(stringStatement);
		return insertStatement.executeInsert();
	}
	
	/**
	 * Given name and value array and compose a proper insert statement
	 * 
	 * @param tableName
	 * @param names
	 * @param values
	 * @return a proper insert statement with column names and corresponding values
	 * @throws JavoidAppException
	 */
	public String getInsertStatement(String tableName, String names[], String values[]) 
			throws JavoidAppException {
		StringBuilder sb = new StringBuilder();
		if (names.length == values.length) {
			sb.append("INSERT INTO ");
			sb.append(tableName);
			appendParameters(names, sb);
			sb.append(" ");
			sb.append("VALUES");
			appendParameters(values, sb);
		} else {
			throw new JavoidAppException("Having different numbers of column name and value...");
		}
		
		return sb.toString();
	}

	/**
	 * Helper method to iteratively append params
	 * 
	 * @param params
	 * @param sb
	 */
	private void appendParameters(String[] params, StringBuilder sb) {
		sb.append(" (");
		for(int i=0; i<params.length; i++) {
			sb.append(params[i]);
			if (i == params.length-1) {
				sb.append(")");
			} else {
				sb.append(", ");
			}
		}
	}
	
	/**
	 * DBHelper to manipulating DB
	 * 
	 * @author Zeyin
	 */
	private static class DBHelper extends SQLiteOpenHelper {

		  DBHelper(Context context) {
	         super(context, DBConstant.DATABASE_NAME, null, DBConstant.DATABASE_VERSION);
	      }

	      @Override
	      public void onCreate(SQLiteDatabase db) {
	    	  
	      }

	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    	  
	      }
	      
	      @Override
	      public void onOpen(SQLiteDatabase db) throws SQLException{
	      	  
	    	  //Open the database
	    	  String myPath = DBConstant.DATABASE_PATH + DBConstant.DATABASE_NAME;
	    	  db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		  }
	   }
}
